<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<h1>登录</h1>

<form action="." method="post" onsubmit="encryptCredentials()">
    <fieldset style="background-color: cyan">
        <label for="username">昵称
            <input type="text" id="username" name="username"
                   value="${cookie.username.value}" required>
        </label>
        <br>
        <label for="password">密码
            <input type="password" id="password"
                   name="password" value="${cookie.password.value}"
                   required>
        </label>
        <br>
        <label>
            <input type="checkbox" name="rememberMe" ${cookie.username != null ? "checked" : ""}>
            记住我的消息
        </label>
        <br>
        <button type="submit">登录</button>
    </fieldset>
</form>

<p>没有账号？<a href="register">先注册</a></p>

<c:if test="${param.error != null}">
    <p style="color:red;">${param.error}</p>
</c:if>
<script>
    const shiftKey = [1, 2, 3, 5, 4]; // 密钥数组
    function encryptCredentials() {
        const usernameField = document.getElementById("username");
        const passwordField = document.getElementById("password");

        const username = usernameField.value;
        const password = passwordField.value;
        console.log(password)
        console.log(username)
        let encryptedUsername = "";
        let encryptedPassword = "";
        for (let i = 0; i < username.length; i++) {
            let charCode = username.charCodeAt(i);
            charCode += shiftKey[i % shiftKey.length];
            encryptedUsername += String.fromCharCode(charCode);
        }
        for (let i = 0; i < password.length; i++) {
            let charCode = password.charCodeAt(i);
            charCode += shiftKey[i % shiftKey.length];
            encryptedPassword += String.fromCharCode(charCode);
        }
        console.log(encryptedPassword)
        console.log(encryptedUsername)
        // 简单加密逻辑（Base64）
        usernameField.value = encryptedUsername;
        passwordField.value = encryptedPassword;
        alert("!")
    }

    function decryptCredentials() {
        const usernameField = document.getElementById("username");
        const passwordField = document.getElementById("password");

        const encryptedUsername = usernameField.value;
        const encryptedPassword = passwordField.value;
        let username = decrypt(encryptedUsername);
        let password = decrypt(encryptedPassword);

        usernameField.value = username;
        passwordField.value = password;
        alert('...')
    }

    function encrypt(text) {
        let encryptedText = "";
        for (let i = 0; i < text.length; i++) {
            let charCode = text.charCodeAt(i);
            charCode += shiftKey[i % shiftKey.length];
            encryptedText += String.fromCharCode(charCode);
        }
        return encryptedText;
    }

    function decrypt(text) {
        let decryptedText = "";
        for (let i = 0; i < text.length; i++) {
            let charCode = text.charCodeAt(i);
            charCode -= shiftKey[i % shiftKey.length];
            decryptedText += String.fromCharCode(charCode);
        }
        return decryptedText;
    }

    decryptCredentials();
</script>

</body>
</html>
